from django.db import models
from django.contrib.auth.models import AbstractUser
from stdimage import StdImageField
from django.utils.safestring import mark_safe

class UserModel(AbstractUser):
    CHOICE_LEVEL = (
        (0,'普通用户'),
        (1,'会员'),
    )
    phone = models.CharField(max_length=11,unique=True,verbose_name='手机号')
    avatar = StdImageField(upload_to='user/',null=True,blank=True,default='user/default.jpg',verbose_name='头像',
                           variations={
                               'thumb_200x200': {'width': 200, 'height': 200},  # 'large': (600, 400),
                               'thumb_100x100': {'width': 100, 'height': 100},  # 'medium': (300, 200),
                               'thumb_50x50': {'width': 50, 'height': 50}  # 'thumbnail': (100, 100),
                           }
                           )
    score = models.IntegerField(default=0,blank=True,null=True,verbose_name='积分')
    level = models.IntegerField(choices=CHOICE_LEVEL,default=0,blank=True,verbose_name='用户等级')


    def __str__(self):
        return self.username

    class Meta:
        db_table = 'dx_user'
        verbose_name = '用户表'
        verbose_name_plural = '用户表'

    @property
    def avatar_display(self):
        return "http://127.0.0.1:8000/{}".format(self.avatar.thumb_100x100.url)

    def avatar_small(self):
        return mark_safe(f'<img style="border-radius:100%" alt="" src="{self.avatar.thumb_50x50.url}"></img>')

    # 图片的描述
    avatar_small.short_description = '用户图片'
    # 图片是否显示
    avatar_small.allow_tags = True
    # 设置排序顺序
    avatar_small.admin_order_field = "avatar"
